﻿using System;
using System.Collections;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Input;

using SBPweb.RIA.Client.Controls.Base;

namespace SBPweb.PlAzureNet.Client.Library.Controls
{

	public partial class ToolBarButton : UserControlBase
	{

		public ToolBarButton()
		{
			// Required to initialize variables
			InitializeComponent();
		}

        public static readonly DependencyProperty ButtonFaceProperty = DependencyProperty.Register("ButtonFace", typeof(Color), typeof(ToolBarButton), new PropertyMetadata(Color.FromArgb(0xFF, 0x7C, 0xC5, 0xFA)));
        public static readonly DependencyProperty ButtonFaceHoverProperty = DependencyProperty.Register("ButtonFaceHover", typeof(Color), typeof(ToolBarButton), new PropertyMetadata(Color.FromArgb(0xFF, 0xFF, 0xFF, 0xFF)));
        public static readonly DependencyProperty ButtonFacePressedProperty = DependencyProperty.Register("ButtonFacePressed", typeof(Color), typeof(ToolBarButton), new PropertyMetadata(Color.FromArgb(0xCC, 0xFF, 0xFF, 0xFF)));
        public static readonly DependencyProperty ButtonContentProperty = DependencyProperty.Register("ButtonContent", typeof(object), typeof(ToolBarButton), null);
        public static readonly DependencyProperty ContentTypeNameProperty = DependencyProperty.Register("ContentTypeName", typeof(string), typeof(ToolBarButton), null);

        public Color ButtonFace
        {
            get
            {
                return GetValue<Color>(ButtonFaceProperty);
            }
            set
            {
                SetValue<Color>(ButtonFaceProperty, value, "ButtonFace");
            }
        }

        public Color ButtonFaceHover
        {
            get
            {
                return GetValue<Color>(ButtonFaceHoverProperty);
            }
            set
            {
                SetValue<Color>(ButtonFaceHoverProperty, value, "ButtonFaceHover");
            }
        }

        public Color ButtonFacePressed
        {
            get
            {
                return GetValue<Color>(ButtonFacePressedProperty);
            }
            set
            {
                SetValue<Color>(ButtonFacePressedProperty, value, "ButtonFacePressed");
            }
        }

        public object ButtonContent
        {
            get
            {
                return GetValue<object>(ButtonContentProperty);
            }
            set
            {
                SetValue<object>(ButtonContentProperty, value, "ButtonContent");
                cp.Content = value;
            }
        }

        public string ContentTypeName
        {
            get
            {
                return GetValue<string>(ContentTypeNameProperty);
            }
            set
            {
                SetValue<string>(ContentTypeNameProperty, value, "ContentTypeName");
                ContentType = Type.GetType(string.Format("{0}.{1}", this.GetType().Namespace, value));
            }
        }

        public Type ContentType
        {
            get;
            private set;
        }

        public new event MouseEventHandler MouseEnter
        {
            add
            {
                grd.MouseEnter += value;
            }
            remove
            {
                grd.MouseEnter += value;
            }
        }

        public new event MouseEventHandler MouseLeave
        {
            add
            {
                grd.MouseLeave += value;
            }
            remove
            {
                grd.MouseLeave += value;
            }
        }

        public event MouseButtonEventHandler MouseClick
        {
            add
            {
                grd.MouseLeftButtonUp += value;
            }
            remove
            {
                grd.MouseLeftButtonUp += value;
            }
        }

        protected override void OnLoaded(object sender, RoutedEventArgs e)
        {
            base.OnLoaded(sender, e);
            var vsgs = VisualStateManager.GetVisualStateGroups(grd).Cast<VisualStateGroup>();
            ColorAnimation animNormal = vsgs.FirstOrDefault().States.Cast<VisualState>().FirstOrDefault(x => x.Name == "Normal").Storyboard.Children[0] as ColorAnimation;
            ColorAnimation animHover = vsgs.FirstOrDefault().States.Cast<VisualState>().FirstOrDefault(x => x.Name == "Hover").Storyboard.Children[0] as ColorAnimation;
            ColorAnimation animPressed = vsgs.FirstOrDefault().States.Cast<VisualState>().FirstOrDefault(x => x.Name == "Pressed").Storyboard.Children[0] as ColorAnimation;
            animNormal.To = ButtonFace;
            animHover.To = ButtonFaceHover;
            animPressed.To = ButtonFacePressed;
            VisualStateManager.GoToState(this, "Normal", true);
        }

        private void rect_MouseEnter(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "Hover", true);
        }

        private void rect_MouseLeave(object sender, MouseEventArgs e)
        {
            VisualStateManager.GoToState(this, "Normal", true);
        }

        private void rect_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            VisualStateManager.GoToState(this, "Pressed", true);
        }

	}

}